<?php
class Seo_SiteController extends Promotor_Controller_Action_Scaffold {
	
	public $skin = array(
		// forsuje domyslny template i style na admin
		'template' => 'admin',
		'style' => 'default',
		// layout
		'layout' => 'admin_seo_site',
		// rozszerzam konfiguracje
		'config' => array(
			'filename' => 'backend_config.ini'
		)
	);
	
	public $acl = array(
		'list' => 'list',
		'add' => 'create',
		'edit' => 'update',
		'delete' => 'delete'
	);
	
	/**
	 * @see Promotor_Controller_Action_Scaffold specialization
	 */
	protected $_modelClass = 'Seo_Model_Site';
	protected $_formAddClass = 'Seo_Form_SiteAdd';
	protected $_formEditClass = 'Seo_Form_SiteEdit';
	protected $_formRemoveClass = 'Seo_Form_SiteRemove';
	
	public function indexAction() {
		$this->_forward('list');
	}

	public function displayAction() {
		$model = new Seo_Model_Site();
		$table = $model->getDbTable();
		
		/* @var $rq Zend_Controller_Request_Http */
		$rq = $this->getRequest();
		
		if ($this->_hasParam('id')) {
			$result = $model->fetchAllByIdCache($this->_getParam('id'));
		} else
		if ($this->_hasParam('alias')){
			$result = $model->fetchAllByAliasCache($this->_getParam('alias'));
		} else {
			$this->_helper->viewRenderer->render('display.404');
			return;
		}

		if ($model->getStatus() === Seo_Model_Site::FAILURE) {
			$this->_helper->viewRenderer->render('display.404');
			return;
		}
		
		list($row, $rowset) = $result;
		$this->view->row = $row;
		$this->view->rowset = $rowset;
		
		// znaleziono jeden rekord to po co przepłacać.. natychmiastowe wyświetlenie tego rekordu
		if (count($rowset) == 1) {
			$current = current($rowset);

			// parametry
			$params = $this->_getAllParams();
			$params['id'] = $current['id'];

			switch ($current['type']) {
				default:
				case 'image':
					$this->_forward('display','image','gallery', $params);
					break;
				case 'media':
					$this->_forward('display','media','gallery', $params);
					break;
			}

			return;
		}
		
		$type = strtolower($this->_getParam('type', @$row['display_type']));
		
		switch ($type) {
			case 'jshorizont':
			case 'autoviewer':
			case 'simpleviewer':
			case 'tiltviewer':
			case 'postcardviewer':
				$this->view->type = $type;
				$this->_helper->viewRenderer->render('display.' . $type);
				break;
		}
	}
	
	public function listAction() {
		$model = new Seo_Model_Site();
		$table = $model->getDbTable();

		$rq = $this->getRequest();
		if ($rq->isPost()) {
			switch ($rq->getPost('action_type')) {
				case 'update':
					if (null !== $rq->getPost('editable')) {
						if ($this->_helper->acl->isAllowed('update')) {
							$data = $rq->getPost('editable');
							$model->editableUpdate($data);
							$this->_helper->flashMessenger($model->getStatus());
						}
					}
					break;
				case 'delete':
					if (null !== $rq->getPost('action_checked')) {
						if ($this->_helper->acl->isAllowed('delete')) {
							$data = $rq->getPost('action_checked');
							$model->editableDelete($data);
							$this->_helper->flashMessenger($model->getStatus());
						}
					}
					break;
			}			
		}

		// setup data grid
		$config = $this->_helper->config('album.xml');
		$grid = KontorX_DataGrid::factory($model->selectList(), $config->grid);
		$grid->setValues($this->_getAllParams());
		
		$this->_setupDataGridPaginator($grid);

		$this->view->grid = $grid;
	}
	
	
	public function editAction() {
		/* @var $model Seo_Model_Site */
		$model = new $this->_modelClass();
		$dbTable = $model->getDbTable();

		$form = new $this->_formEditClass(array(
			'primaryKey' => $this->_getParam('id'),
			'treeValues' => $model->getAttachedSitesId($this->_getParam('id'))
		));

		$scaffold = $this->_helper->getHelper('scaffold');
		$scaffold
			->setDbTable($dbTable)
			->setForm($form)
			->setRowPk($this->_getAllParams())
			->run(KontorX_Controller_Action_Helper_Scaffold::UPDATE);

		$status = $scaffold->getStatus();
		
		/* @var $falshMessages Zend_Controller_Helper_FlashMessenger */
		$falshMessages = $this->_helper->getHelper('flashMessenger');

		if ($status === KontorX_Controller_Action_Helper_Scaffold::SUCCESS) {
			$falshMessages->addMessage($status);

			$primaryKey = $scaffold->getRowPK();
			$this->_noticeObserver('post', $primaryKey);

			$this->_helper->redirector->goTo('edit',null,null,$primaryKey);
		} else
		if ($status === KontorX_Controller_Action_Helper_Scaffold::NO_EXSISTS) {
			$falshMessages->addMessage($status);
			$this->_helper->redirector->goTo('list');
		} else {
			$falshMessages->addMessage($status);
			$this->view->form = $form;
		}
	}
}